<?php

namespace app\common\func;

use app\common\model\Allocation;
use app\common\model\Equip;
use app\common\model\FlowSft;
use app\common\model\transaction\Hk;
use app\common\model\transaction\Jl;
use app\common\model\transaction\Sft;
use app\common\model\transaction\Xyf;
use app\common\model\transaction\Ys;
use think\Db;

class Transaction {

    public static function getEquipFlowTime($productId,$sn,Equip $equip){
        switch ($productId){
            case 6:
                $order = Xyf::where(["sn" => $sn,"is_flow" => 1])->find();
                break;
            case 8:
            case 3:
                $order = FlowSft::where(["sn" => $sn])->find();
                break;
            case 5:
            case 4:
                //海科计算流量卡:绑定时间的第二天，的第一笔大于100的交易
                $startTime = strtotime(date("Y-m-d",strtotime("+1 day",$equip->binding_time)));
                $conditionMoney = 100;
                $order = Hk::where(["sn" => $sn])->where("trading_time",">=",$startTime)->where("transaction_amount",">=",$conditionMoney)->order("trading_time asc")->find();
                break;
            default:
                return 0;
        }

        return $order ? $order->trading_time : 0;
    }

    public static function getSftDataList(){
        $timeStamp = strtotime(date("Y-m-d"));
        return Sft::where(["handle_state" => 1])->where(["trading_time" => ["<",$timeStamp]])->order("trading_time asc")->select();
    }

    public static function getHkDataList(){
        return Hk::where(["handle_state" => 1])->order("trading_time asc")->select();
    }

    public static function getJlDataList(){
        $timeStamp = strtotime(date("Y-m-d"));
        return Jl::where(["handle_state" => 1])->where(["trading_time" => ["<",$timeStamp]])->order("trading_time asc")->select();
    }

    public static function getYsDataList(){
        $timeStamp = strtotime(date("Y-m-d"));
        return Ys::where(["handle_state" => 1])->where(["trading_time" => ["<",$timeStamp]])->order("trading_time asc")->select();
    }

    public static function getDataList(){
        return Xyf::where(["handle_state" => 1])->order("trading_time asc")->select();
    }

    //--------------------------user查询上下级-------------------------------
    public static function getRangeList($sn){
        $first = Lib::getEquip($sn);
        if($first){
            return self::getSuper($first->main_type,$first->main_id);
        }else{
            return [];
        }
    }

    public static function getSuper($mainType, $mainId){
        if($mainType == 1){
            $obj = Lib::getCompany($mainId);
            $type = 1;
        }else{
            $obj = Lib::getUserInfo($mainId);
            $type = 2;
        }
        $list = [];
        if($obj){
            $list[] = ["type" => $type,"id" => $obj->id];
            $upper = self::getSuper($mainType == 1 ? 1 : $obj->ptype,$obj->pid);
            if($upper){
                $list = array_merge($list,$upper);
            }
            return $list;
        }else{
            return [];
        }
    }
    //--------------------------user查询上下级-------------------------------

    //--------------------------调拨记录查询上下级-------------------------------

    public static function getRange($sn){
        $range = [];
        $first = Lib::getLevelInfo($sn);
        if($first){
            $range[] = ["type" => $first['to_type'],"id" => $first['to'],"policy_id" => $first['policy_id']];
            $super = self::getSuperList($first['from_type'],$first['from'],$sn);
            return array_merge($range,$super);
        }else{
            return [];
        }
    }

    public static function getSuperList($mainType,$mainId,$sn){
        $list = [];
        $data = Allocation::where(["to_type" => $mainType,"to" => $mainId])->find();
        if($data){
            $list[] = ["type" => $data->to_type,"id" => $data->to,"policy_id" => $data->policy_id];
            $upper = self::getSuperList($data->from_type,$data->from,$sn);
            if($upper){
                $list = array_merge($list,$upper);
            }else{
                $stock = Lib::getStockConfirm($data->from,$sn);
                if($stock){
                    $list = array_merge($list,[["type" => $data->from_type,"id" => $data->from,"policy_id" => $stock['policy_id']]]);
                }
            }
            return $list;
        }else{
            return [];
        }
    }

//    public static function getSuperList($mainType,$mainId,$sn){
//        $list = [];
//        $data = Db::name("allocation")->alias("a")
//            ->join("allocation_details d","a.id = d.allocation_id","LEFT")
//            ->where(["a.to_type" => $mainType,"a.to" => $mainId,"d.sn" => $sn])
//            ->find();
//        if($data){
//            self::getSuperList($data['from_type'],$data['from'],$sn);
//            $list = array_merge($list,[["type" => $data['to_type'],"id" => $data['to'],"policy_id" => $data['policy_id']]]);
//        }else{
//            $stock = Lib::getStockConfirm($mainId,$sn);
//            if($stock){
//                $list = array_merge($list,[["type" => $mainType,"id" => $mainId,"policy_id" => $stock['policy_id']]]);
//            }
//        }
//        return $list;
//    }

    //--------------------------调拨记录查询上下级-------------------------------

    public static function roundNumber($money, $percent){
        return floor($money * $percent / 100 ) / 100;
    }

    public static function formatNumber($number){
        return floor($number * 100 ) / 100;
    }

    public static function getCardTypeId($type){
        $arr = array_flip(Tools::$cardType);
        return $arr[$type] ? $arr[$type] : false;
    }
}
